<template>
  <el-card>
    <div class="box">
        <div class="box-row-1">
            <el-form :model="seacrhform" ref="seacrhform">
                <div class="box-form">
                    <el-form-item label="岗位编码" class="displayClass">
                        <el-input v-model="seacrhform.post_code" placeholder="请输入岗位编码"></el-input>
                    </el-form-item>
                    <el-form-item label="岗位名称" class="displayClass">
                        <el-input v-model="seacrhform.post_name" placeholder="请输入岗位名称"></el-input>
                    </el-form-item>
                    <el-form-item label="状态" class="displayClass">
                        <el-select v-model="seacrhform.status" placeholder="岗位状态" clearable>
                            <el-option v-for="item in options" :key="item.value"
                                :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item>
                        <el-button class="el-button--mini" type="primary" icon="el-icon-search" @click="submitForm('ruleForm')">搜索</el-button>
                        <el-button class="el-button--mini" @click="resetForm('ruleForm')" icon="el-icon-refresh">重置</el-button>
                    </el-form-item>
                </div>
            </el-form>
        </div>

        <div class="box-row-2">
            <el-row>
                <el-col :span="12">
                    <div class="grid-content bg-purple">
                        <el-button size="small" type="primary" icon="el-icon-plus" plain @click="add">新增</el-button>
                        <el-button size="small" type="success" icon="el-icon-edit" :disabled="isUDisable" @click="handleEdit(null,true)" plain>修改</el-button>
                        <el-button size="small" type="danger" icon="el-icon-delete" @click="delAll" :disabled="isDisable" plain>删除</el-button>
                    </div>
                </el-col>
                <el-col :span="12">
                    <div class="grid-content bg-purple-light">
                        <el-button icon="el-icon-search"  size="small" circle></el-button>
                        <el-button icon="el-icon-refresh" size="small" circle></el-button>
                    </div>
                </el-col>
            </el-row>
        </div>

        <div class="box-row-3">
            <el-table 
            :data="tableData"
            ref="dataTable"
            height="calc(100vh - 280px)" 
            v-loading="isloading"
            @selection-change="handleSelectionChange"
            style="width: 100%;margin-bottom: 20px;">
                <el-table-column type="selection" width="55"></el-table-column>
                <el-table-column
                    prop="post_code"
                    label="岗位编码" width="200">
                </el-table-column>
                <el-table-column
                    prop="post_name"
                    label="岗位名称" width="200">
                </el-table-column>
                <el-table-column
                    prop="post_sort"
                    label="岗位排序" width="200">
                </el-table-column>
                <el-table-column prop="status" label="状态" width="80" align="center">
                        <template slot-scope="scope">
                            <el-tag :type="scope.row.status == 0 ? 'primary' : 'danger'" disable-transitions>
                                <div v-if="scope.row.status == 0">正常</div>
                                <div v-else>停用</div>
                            </el-tag>
                        </template>
                </el-table-column>
            
                <el-table-column prop="create_time" label="创建时间" width="250" align="center" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column fixed="right" label="操作" align="center" >
                    <template slot-scope="scope">
                        <el-button @click="handleEdit(scope.row)" type="text" size="small" icon="el-icon-edit">编辑</el-button>
                        <el-button @click="handleDel(scope.row)" type="text" size="small" icon="el-icon-delete">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="box-row-3-pagination">
                <Pagination  :pageindex="PageIndex" :pagesize="PageSize" :total="total"  @CurrentChange="CurrentChange" @SizeChange="SizeChange" />
            </div>
        </div>
    </div>
    <el-dialog width="450px" class="box-dialog" :title="title" :visible.sync="dialogFormVisible" @close="closeDialog" :close-on-click-modal="false">
        <AddPost :model="model" :isUpdate="isUpdate" @Closedialog="closeDialog" @dataBind="dataBind"/>
    </el-dialog>
  </el-card>
</template>

<script>
import AddPost from './components/AddPost.vue'
import Pagination from '../../components/Pagination.vue'
export default {
    components:{
        Pagination,
        AddPost
    },
    data(){
        return{
            options:[{
                    value: '0',
                    label: '正常'
                },{
                    value:'1',
                    label:'停用'
                }
            ],
            seacrhform:{
                post_name:'',
                status:'',
                post_code:''
            },
            model:{
                post_code:'',
                post_name:'',
                post_sort:'',
                status:'',
                remark:'',
            },
            tableData: [],
            title:'新增岗位',
            isUpdate:false,//是否修改
            isloading:false,
            dialogFormVisible:false, //是否显示弹窗
            isDisable:true,//是否禁用按钮
            isUDisable:true,//是否禁用修改按钮
            PageIndex:1,//当前页数
            PageSize:10,//每页显示条数
            total:0,//总条数
        }
    },
    methods:{
        submitForm(formName){
           this.dataBind();
        },
        resetForm(formName){
            this.$refs[formName].resetFields()
        },
        add(){
            this.title='新增岗位'
            this.dialogFormVisible=true;
            this.isUpdate=false
        },
        //编辑
        handleEdit(model,isHeadsU=false){
            if(isHeadsU){
                this.model = JSON.parse(JSON.stringify(this.$refs.dataTable.selection[0]));
            }
            else{
                this.model=JSON.parse(JSON.stringify(model));
            }
            this.title="修改岗位";
            this.dialogFormVisible = true;
            this.isUpdate = true;
        },
        //删除多条数据
        delAll(){
            if(this.$refs.dataTable.selection.length > 0){
                let ids = this.$refs.dataTable.selection.map(x=>x.post_id);
                console.log(ids);
                this.$confirm('确认删除'+this.$refs.dataTable.selection.length+'条数据吗？',' 是否继续?','提示',{
                    confirmButtonText:'确定',
                    cancelButtonText:'取消',
                    type:'warning'
                })
                .then(()=>{
                    const loading =this.$loading();
                    this.$http.delete('/api/PostManage/DelAll',{params:{post_ids:ids.join(',')}})
                        .then(res =>{
                            if(res.data.code == 200){
                                this.ShowMsg(res.data.msg,'success');
                                this.dataBind();
                            }else{
                                this.ShowMsg(res.data.msg,'warning');
                            }
                        }).catch(err=>{
                            this.ShowMsg('服务器繁忙，请稍后重试','error');
                        }).finally(()=>{
                            setTimeout(()=>{
                                loading.close();
                            },500);
                        })
                }).
                catch(()=>{})
            }
        },
        //删除单条
        handleDel(e){
            let Id =e.post_id
            if(Id != undefined){
                this.$confirm('此操作将永久删除数据, 是否继续?','提示',{
                    confirmButtonText:'确定',
                    cancelButtonText:'取消',
                    type:'warning'
                })
                .then(()=>{
                    const loading = this.$loading();
                    this.$http.delete('/api/PostManage',{params:{post_id:Id}})
                        .then(res =>{
                            if(res.data.code == 200){
                                this.ShowMsg(res.data.msg,'success');
                                this.dataBind();
                            }
                            else{
                                this.ShowMsg(res.data.msg,'warning');
                            }
                        }).catch(err =>{
                            this.ShowMsg('服务器异常，请稍后重试','error');
                        }).finally(()=>{
                            setTimeout(()=>{
                                loading.close();
                            },500);
                        })
                }).catch(()=>{})
            }
        },
        dataBind(){
            this.isloading=true
            this.$http.request({
                type:'get',
                url:'/api/PostManage',
                params:{
                    PageIndex:this.PageIndex,
                    PageSize:this.PageSize,
                    post_code:this.seacrhform.post_code,
                    post_name:this.seacrhform.post_name,
                    status:this.seacrhform.status
                }
            }).then(res=>{
                console.log(res.data);
                if(res.data.code==200){
                    this.tableData=res.data.data;
                    // let datas = res.data.data;
                    // this.optionTree= this.cvtTreeData(datas);
                    this.total=res.data.count
                }
            }).catch(err=>{
                this.ShowMsg('服务器异常','error');
                console.log(err)
            }).finally(()=>{
                this.isloading=false;
            })
        },
        SizeChange(e){
            this.PageSize = e;
            console.log('1');
            //从第一页开始
            this.PageIndex = 1;
            this.dataBind();
        },
        CurrentChange(e){
            console.log('1');
            this.PageIndex = e;
            this.dataBind();
        },
        closeDialog(){
            this.model={
                post_code:'',
                post_name:'',
                post_sort:'',
                status:'',
                remark:'',
            },
            this.dialogFormVisible=false;
        },
          //监听dataTable是否有选中项
          handleSelectionChange(val){
            if(val.length > 0){
                this.isDisable = false;
            }
            else if(!val.length){
                this.isDisable =true;
            }
            let isTrue = val.length ===1 ? false:true;
            this.isUDisable= isTrue;
        },
    },
    created(){
        this.dataBind();
    }
    
}
</script>

<style>
.box-form{
    display: flex;
    align-items: center;
}
.displayClass{
    display: flex;
    margin-right:15px;
}
.displayClass input{
    width:235px;
    height:35px;
}
.el-button--mini{
    padding:10px 20px;
}
.bg-purple-light{
    margin-right: 10px;
    display: flex;
    justify-content: flex-end;
}
.box-row-3{
    margin-top:10px;
}
.box-Input-mini input,.box-Input-mini textarea{
    width:100%;
}
.box-Input-mini textarea{
    height:60px;
}
.el-button--mini{
    padding:10px 20px;
}
.bg-purple-light{
    margin-right: 10px;
    display: flex;
    justify-content: flex-end;
}
.box-row-3,.box-row-3-pagination{
    margin-top:10px;
}
</style>